namespace PMS.BLL
{
    using PMS.Common;
    using PMS.DBUtility;
    using PMS.Model;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.SqlClient;
    using System.Runtime.InteropServices;
    using System.Text;
    using System.Threading;

    public class SyslogBLL
    {
        public static void Add(Syslog model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into Syslog(");
            builder.Append("LogType,LogUser,LogContent,LogDate,LogIP,LogClient");
            builder.Append(") values (");
            builder.Append("@LogType,@LogUser,@LogContent,@LogDate,@LogIP,@LogClient");
            builder.Append(") ");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@LogType", model.LogType), new SqlParameter("@LogUser", model.LogUser), new SqlParameter("@LogContent", model.LogContent), new SqlParameter("@LogDate", model.LogDate), new SqlParameter("@LogIP", model.LogIP), new SqlParameter("@LogClient", model.LogClient) };
            object single = DBHelper.GetSingle(builder.ToString(), cmdParms);
        }

        public bool Delete(int LogID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from Syslog ");
            builder.Append(" where LogID=@LogID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@LogID", SqlDbType.Int, 4) };
            cmdParms[0].Value = LogID;
            return (DBHelper.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }

        public bool DeleteList(string LogIDlist)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from Syslog ");
            builder.Append(" where ID in (" + LogIDlist + ")  ");
            return (DBHelper.ExecuteSql(builder.ToString()) > 0);
        }

        public List<Syslog> GetList(int pageIndex, int pageSize, out int totalCount)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append(" select  count(LogID) from Syslog ");
            totalCount = Convert.ToInt32(DBHelper.Query(builder.ToString()).Tables[0].Rows[0][0].ToString());
            builder.Clear();
            if (pageIndex == 1)
            {
                builder.Append(" select top " + pageSize + " * from Syslog  order by LogID DESC ");
            }
            else
            {
                builder.Append(" select top " + pageSize + " * from Syslog where LogID  < ");
                builder.Append(" (select  case when MIN(LogID) is null then 0 else MIN(LogID) end");
                builder.Append(" from (select top " + ((pageIndex - 1) * pageSize) + " LogID from Syslog order by LogID DESC)t)");
                builder.Append(" order by LogID DESC");
            }
            DataTable table = DBHelper.Query(builder.ToString()).Tables[0];
            Syslog item = null;
            List<Syslog> list = null;
            if (table.Rows.Count > 0)
            {
                list = new List<Syslog>();
                foreach (DataRow row in table.Rows)
                {
                    item = new Syslog();
                    if (row["LogID"].ToString() != "")
                    {
                        item.LogID = int.Parse(row["LogID"].ToString());
                    }
                    item.LogType = row["LogType"].ToString();
                    item.LogUser = row["LogUser"].ToString();
                    item.LogContent = row["LogContent"].ToString();
                    if (row["LogDate"].ToString() != "")
                    {
                        item.LogDate = DateTime.Parse(row["LogDate"].ToString());
                    }
                    item.LogIP = row["LogIP"].ToString();
                    item.LogClient = row["LogClient"].ToString();
                    list.Add(item);
                }
            }
            return list;
        }

        public Syslog GetModel(int LogID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select LogID, LogType, LogUser, LogContent, LogDate, LogIP, LogClient  ");
            builder.Append("  from Syslog ");
            builder.Append(" where LogID=@LogID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@LogID", SqlDbType.Int, 4) };
            cmdParms[0].Value = LogID;
            Syslog syslog = new Syslog();
            DataSet set = DBHelper.Query(builder.ToString(), cmdParms);
            if (set.Tables[0].Rows.Count > 0)
            {
                DataRow row = set.Tables[0].Rows[0];
                if (row["LogID"].ToString() != "")
                {
                    syslog.LogID = int.Parse(row["LogID"].ToString());
                }
                syslog.LogType = row["LogType"].ToString();
                syslog.LogUser = row["LogUser"].ToString();
                syslog.LogContent = row["LogContent"].ToString();
                if (row["LogDate"].ToString() != "")
                {
                    syslog.LogDate = DateTime.Parse(row["LogDate"].ToString());
                }
                syslog.LogIP = row["LogIP"].ToString();
                syslog.LogClient = row["LogClient"].ToString();
                return syslog;
            }
            return null;
        }

        public bool Update(Syslog model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update Syslog set ");
            builder.Append(" LogType = @LogType , ");
            builder.Append(" LogUser = @LogUser , ");
            builder.Append(" LogContent = @LogContent , ");
            builder.Append(" LogDate = @LogDate , ");
            builder.Append(" LogIP = @LogIP , ");
            builder.Append(" LogClient = @LogClient  ");
            builder.Append(" where LogID=@LogID ");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@LogID", model.LogID), new SqlParameter("@LogType", model.LogType), new SqlParameter("@LogUser", model.LogUser), new SqlParameter("@LogContent", model.LogContent), new SqlParameter("@LogDate", model.LogDate), new SqlParameter("@LogIP", model.LogIP), new SqlParameter("@LogClient", model.LogClient) };
            return (DBHelper.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }

        public static void Write(string logType, string content)
        {
            ThreadPool.QueueUserWorkItem(delegate (object obj) {
                try
                {
                    Syslog model = new Syslog {
                        LogType = logType,
                        LogContent = content,
                        LogDate = DateTime.Now,
                        LogClient = "Client",
                        LogIP = IPHelper.GetIP(),
                        LogUser = Keys.Admin.LoginName
                    };
                    Add(model);
                }
                catch (Exception exception)
                {
                    Log.Write(exception.Message);
                }
            });
        }
    }
}

